package org.OpenGeoPortal.Download;

import java.io.IOException;

import org.json.simple.JSONObject;
import org.json.simple.parser.ParseException;

public class SolrConfigRetriever extends ConfigRetriever implements
		SearchConfigRetriever, OGCServiceRetriever {
	public SolrConfigRetriever(String configFilePath){
		this.setConfigFilePath(configFilePath);
	}

	public String getSearchUrl() throws ParseException, IOException{
		this.readConfigFile();
		JSONObject jsonObject = (JSONObject) this.configContents.get("search");
		return (String) jsonObject.get("serviceAddress");
	}
	
	public String getSearchPort() throws ParseException, IOException{
		this.readConfigFile();
		JSONObject jsonObject = (JSONObject) this.configContents.get("search");
		Long searchPort = (Long) jsonObject.get("servicePort");
		return searchPort.toString();
	}
	
	public String getSearchType() throws ParseException, IOException{
		this.readConfigFile();
		JSONObject jsonObject = (JSONObject) this.configContents.get("search");
		return (String) jsonObject.get("serviceType");
	}
	
	public String getHome() throws ParseException, IOException{
		this.readConfigFile();
		return (String) this.configContents.get("homeInstitution");
	}
	
	public Boolean isOgp(){
		return true;
	}

	public String getWMSServicePoint(String institution) throws Exception{
		return this.getOGCServicePoint("WMS", institution);
	}
	
	public String getWFSServicePoint(String institution) throws Exception{
		return this.getOGCServicePoint("WFS", institution);
	}
	
	public String getWCSServicePoint(String institution) throws Exception{
		return this.getOGCServicePoint("WCS", institution);
	}
	
	String getOGCServicePoint(String serviceType, String institution) throws ParseException, IOException{
		this.readConfigFile();
		JSONObject jsonObject = (JSONObject) this.configContents.get("institutions");
		JSONObject institutionsObject = (JSONObject) jsonObject.get(institution);
		JSONObject servicesObject = (JSONObject) institutionsObject.get("OGCServices");
		JSONObject OGCObject = (JSONObject) servicesObject.get(serviceType);

		return (String) OGCObject.get("serviceAddress");
	}
}
